package com.gxa.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.BusinessException;
import com.gxa.pojo.PriceSystem;
import com.gxa.service.PriceSystemService;
import com.gxa.utils.DataCheck;
import com.gxa.utils.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @program: saas
 * @description: 价格体系接口
 * @author: Tirion
 * @create: 2020-09-24 09:31
 **/
@ControllerAdvice
@RequestMapping("/price")
@Controller
@CrossOrigin
@Api(value = "价格体系")
public class PriceSystemController {

    @Resource
    private PriceSystemService priceSystemService;

    @PostMapping("/list")
    @ResponseBody
    @ExceptionHandler(BusinessException.class)
    @ApiOperation(value = "价格列表")
    public Map<String, Object> list(@RequestBody @ApiParam(name = "map",value = "分页信息",required = true) Map<String ,Integer> map) {
        Integer startIndex = map.get("startIndex");
        Integer pageSize = map.get("pageSize");
        PageHelper.startPage(startIndex,pageSize);
        List<PriceSystem> priceSystems = priceSystemService.queryAll();
            JsonResult jsonResult = new JsonResult();
        DataCheck.check(jsonResult,priceSystems);
        PageInfo<PriceSystem> priceSystemPageInfo = new PageInfo<>(priceSystems);
        jsonResult.put("total",priceSystemPageInfo.getTotal());
        return jsonResult.getValues();
    }

    // 查询所有价格体系名称
    @ResponseBody
    @RequestMapping(value = "/findPriceSystem",method = RequestMethod.POST)
    // @ExceptionHandler(BusinessException.class)
    @ApiOperation(value = "查询所有价格体系")
    public Map<String,Object> findPriceSystem(){
        List<Map<String, Object>> priceSystem = priceSystemService.findPriceSystem();
        JsonResult jsonResult = new JsonResult();
        if (priceSystem!=null){
            jsonResult.setCode("200");
            jsonResult.setData(priceSystem);
            jsonResult.setMsg("查询成功！");
        }else {
            jsonResult.setCode("406");
            jsonResult.setMsg("系统开小差了！");
        }
        return jsonResult.getValues();
    }


}
